<?php
class xl_brand_product{
    function read_list(){
        global $dbh;
        $sql='SELECT * FROM brand_products ORDER BY id DESC';
        $sth=$dbh->prepare($sql);
        $sth->execute();
        return $sth->fetchAll();
    }
    function read($id){
        global $dbh;
        $sql='SELECT * FROM brand_products WHERE id=:id LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function insert($data){
        global $dbh;
        $sql='INSERT INTO brand_products (name,alias,title,keyword,description,location,status,image,introduction,address,day_time) 
                VALUE (:name,:alias,:title,:keyword,:description,:location,:status,:image,:introduction,:address,:day_time)';
        $sth=$dbh->prepare($sql);
        $sth->execute($data);
        return $sth;
    }
    function update($data){
        global $dbh;
        if($data['image']!=''){
            $sql='UPDATE brand_products SET name=:name,title=:title,keyword=:keyword,description=:description,introduction=:introduction,location=:location,status=:status,image=:image,address=:address,day_time=:day_time WHERE id=:id';
        } else {
            $sql='UPDATE brand_products SET name=:name,title=:title,keyword=:keyword,description=:description,introduction=:introduction,location=:location,status=:status,address=:address,day_time=:day_time WHERE id=:id';
            unset($data['image']);
        }
        $sth=$dbh->prepare($sql);
        $sth->execute($data);
        return $sth;
    }
    function updatde_status($id){
        global $dbh;
        $sql='UPDATE brand_products SET status=1-status WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
        
    }
    function test($id,$name){
        global $dbh;
        $sql='SELECT * FROM brand_products WHERE id!=:id AND name=:name';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id,':name'=>$name));
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
    function test_name($alias){
        global $dbh;
        $sql='SELECT alias FROM brand_products WHERE alias=:alias';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':alias'=>$alias));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function delete_all($id){
        global $dbh;
        $sql='SELECT * FROM brand_products WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function delete($id){
        global $dbh;
        $sql='DELETE FROM brand_products WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
}